//
//  SegmentMenuView.swift
//  scfriend
//
//  Created by wulanzhou on 2017/9/4.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import UIKit

class SegmentMenuView: UIView {
    
    private var menuArray:[Dictionary<String,String>] = [["image":"signIn","name":"签到"],
                                                         ["image":"experice","name":"经验值"],
                                                         ["image":"task","name":"任务"],
                                                         ["image":"account","name":"帐户"]]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        let menuWidth:CGFloat = frame.size.width/CGFloat(menuArray.count)
        let lineTop:CGFloat = 10
        for index in 0...menuArray.count{
            
            //竖线
            if (index > 0){
                let labLine:UILabel = UILabel.init(frame: CGRect(x: CGFloat(index) * menuWidth - 0.5, y: lineTop, width: 0.5, height: frame.size.height - 2 * lineTop))
                labLine.backgroundColor = UIColor.withRGBA(222, 222, 222)
                self.addSubview(labLine)
            
            }
            
            if(index < menuArray.count){
                
                let item:Dictionary<String,String> = menuArray[index]
                //按钮
                self.createButton(frame: CGRect(x: CGFloat(index) * menuWidth, y: 0, width: menuWidth - 0.5, height: frame.size.height), title: item["name"]!, imageName: item["image"]!, tag: 100 + index)
            }
            
            
        }
        
    }
    
    private func createButton(frame:CGRect,title:String,imageName:String,tag:NSInteger){
    
        let btn = UIButton.init(type: .custom)
        btn.frame = frame
        btn.tag = tag
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
        btn.setImage(UIImage.init(named: imageName), for: .normal)
        btn.setBackgroundImage(UIImage.init(color: UIColor.withHex(hexString: "d9d9d9")), for: .highlighted)
        btn.addTarget(self, action: #selector(menuClick(sender:)), for: .touchUpInside)
        btn.imageEdgeInsets = UIEdgeInsetsMake(-(btn.frame.size.height - btn.titleLabel!.frame.size.height - btn.titleLabel!.frame.origin.y) + 6, (btn.frame.size.width - btn.imageView!.frame.size.width)/2.0, 0, (btn.frame.size.width - btn.imageView!.frame.size.width)/2.0)
        //btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.size.height - btn.imageView!.frame.size.height - btn.imageView!.frame.origin.y, -btn.imageView!.frame.size.width, 0, 0)
        
        btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.size.height  - btn.imageView!.frame.origin.y - 10, -btn.imageView!.frame.size.width, 0, 0)
        
        self.addSubview(btn)
        
    }
    
    @objc fileprivate func menuClick(sender:UIButton){
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
